<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
namespace core;
defined('BY_JYA') or exit('error');
/*
	站点相关函数
*/
class jdk{
	
	public function __construct($uniacid=0){
		if($uniacid==0){
			$uniacid=$_SESSION['uniacid'];
		}
		$this->uniacid=$uniacid;
	}
	
	public function goodsInfo ($sku,$accessToken){
		require ROOT_D.'/core/library/jos-php-open-api-sdk-2.0/jd/JdClient.php';
		
		require ROOT_D.'/core/library/jos-php-open-api-sdk-2.0/jd/request/ServicePromotionGoodsInfoRequest.php';
		$c = new \JdClient();

		$c->appKey = 'F03FC9BEAFACD1FBFD910768C255F661';

		$c->appSecret = '9f0ab0ea2cf64d08b7af5cab6acb6649';

		$c->accessToken =$accessToken;
		$c->serverUrl ='https://api.jd.com/routerjson';

		$req = new \ServicePromotionGoodsInfoRequest();

		$req->setSkuIds($sku);

		$resp = $c->execute($req, $c->accessToken);
		
		return $resp;
	}
	public function geturl ($url,$unionId){
		$time=date('Y-m-d H:i:s',time());
		$p=array(
			'promotionCodeReq'=>array(
			'materialId'=>$url,
			'unionId'=>$unionId,
		));
		$data=array(
			//'access_token'=>'',
			'app_key'=>'f0300e4a141d6c42d41b03b5a3370556',
			//'app_secret'=>'96ea3b8d87214b1f9542df137c6c498e',
			'format'=>'json',
			'method'=>'jd.union.open.promotion.byunionid.get',
			'param_json'=>json_encode($p),
			'sign_method'=>'md5',
			'timestamp'=>$time,
			'v'=>'1.0'
		);
		$str='';
		foreach($data as $k=>$v){
			$str .=$k.$v;
		}
		//print_r($str);exit;
		$str='96ea3b8d87214b1f9542df137c6c498e'.$str.'96ea3b8d87214b1f9542df137c6c498e';
		$str=strtoupper(md5($str));
		$url='https://router.jd.com/api?sign='.urlencode($str).'&sign_method=md5&v=1.0&timestamp='.urlencode($time).'&app_key=f0300e4a141d6c42d41b03b5a3370556&method=jd.union.open.promotion.byunionid.get&format=json&param_json='.urlencode(json_encode($p));
		
		$res=cfc('http')->ihttp_request($url);
		$re=(array)$res['content'];
		$re=json_decode($re['0'],true);
		$re=json_decode($re['jd_union_open_promotion_byunionid_get_response']['result'],true);
		if($re['message']=='success'){
			return $re['data']['shortURL'];
		}else{
			return $re['message'];
		}
	}
	public function getorder ($timestamp,$key){
		$time=date('Y-m-d H:i:s',time());
		$p=array(
			'promotionCodeReq'=>array(
			'pageNo'=>1,
			'pageSize'=>'500',
			'type'=>1,
			'time'=>$timestamp,
			'key'=>$key
		));
		$data=array(
			//'access_token'=>'',
			'app_key'=>'f0300e4a141d6c42d41b03b5a3370556',
			//'app_secret'=>'96ea3b8d87214b1f9542df137c6c498e',
			'format'=>'json',
			'method'=>'jd.union.open.order.query',
			'param_json'=>json_encode($p),
			'sign_method'=>'md5',
			'timestamp'=>$time,
			'v'=>'1.0'
		);
		$str='';
		foreach($data as $k=>$v){
			$str .=$k.$v;
		}
		//print_r($str);exit;
		$str='96ea3b8d87214b1f9542df137c6c498e'.$str.'96ea3b8d87214b1f9542df137c6c498e';
		$str=strtoupper(md5($str));
		$url='https://router.jd.com/api?sign='.urlencode($str).'&sign_method=md5&v=1.0&timestamp='.urlencode($time).'&app_key=f0300e4a141d6c42d41b03b5a3370556&method=jd.union.open.order.query&format=json&param_json='.urlencode(json_encode($p));
		
		$res=cfc('http')->ihttp_request($url);
		$re=(array)$res['content'];
		$re=json_decode($re['0'],true);
		$re=json_decode($re['jd_union_open_order_query_response']['result'],true);
		if($re['message']=='success'){
			return $re['data'];
		}else{
			return $re['message'];
		}
		
	}
}
